package com.mall.seckill_mall.rabbitmq.receiver;

import com.mall.seckill_mall.common.OrderStatus;
import com.mall.seckill_mall.common.RabbitConstants;
import com.mall.seckill_mall.common.RedisPreffix;
import com.mall.seckill_mall.common.SeckillMessage;
import com.mall.seckill_mall.entity.Order;
import com.mall.seckill_mall.service.OrderService;
import com.mall.seckill_mall.service.SeckillService;
import com.mall.seckill_mall.utils.JsonUtils;
import com.rabbitmq.client.Channel;
import jdk.nashorn.internal.parser.JSONParser;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@EnableRabbit
@Configuration
public class MQConsumer {

    @Autowired
    private SeckillService seckillService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    /*
     * 监听死信队列
     */
    @RabbitListener(queues = {RabbitConstants.DLX_QUEUE})
    @RabbitHandler
    public void receiveDeadOrder(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        String orderId = new String(message.getBody());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(new Date()) + "---->" + "死信");
        String orderKey = RedisPreffix.ORDER_PREFFIX.concat(orderId);
        Order order = orderService.getOrderByOrderId(orderId);
        if(order != null && order.getOrderStatus() == OrderStatus.HAVE_NOT_PAID) {
            seckillService.closeSeckill(orderId);
        }
    }

    /*
     * 监听秒杀队列
     */
    @RabbitListener(queues = {RabbitConstants.SEKILL_QUEUE})
    @RabbitHandler
    public void receiveSeckill(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        SeckillMessage seckillMessage = JsonUtils.stringToBean(new String(message.getBody()), SeckillMessage.class);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(new Date()) + "---->下单" + seckillMessage.toString());
        seckillService.seckill(seckillMessage.getUserId(), seckillMessage.getGoods());
    }

//    private static int mod = 0;
//
//    @RabbitListener(queues = {"test_queue"})
//    @RabbitListener
//    public void recTest(Message msg, Channel channel) throws IOException {
//        channel.basicAck(msg.getMessageProperties().getDeliveryTag(), true);
//        if((int) redisTemplate.opsForValue().get("seckill_stock_1") > 0) {
//            redisTemplate.opsForValue().decrement("seckill_stock_1");
//            System.out.println(mod++);
//        }
//    }
}
